<template>
  <el-card shadow='never'>
    <div solt='header'>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house"></i>首页</el-breadcrumb-item>
        <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 内容区域 -->
    <div class='filter-from'>
      <div class='ipt'></div>
      <el-button type='primary'
                 size='small'
                 icon='el-icon-plus'> 增加</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="list"
              border
              style="width: 100%">
      <!-- 折叠列 -->
      <el-table-column type="expand"
                       label="#"
                       width="80">
        <template slot-scope="scope">
          <template v-if='scope.row.children.length'>
            <!-- 去渲染该角色的所有权限 -->
            <el-row class='line'
                    v-for="item in scope.row.children"
                    :key="item.id">
              <el-col :span="8">{{item.authName}}</el-col>
              <el-col :span="16">
                <el-row v-for="val in item.children"
                        :key="val.id">
                  <el-col :span="4">{{val.authName}}</el-col>
                  <el-col :span="20">
                    <el-tag type='warning'
                            closable
                            @close='delTag(v,scope.row)'
                            v-for="v in val.children"
                            :key="v.id">{{v.authName}}</el-tag>
                  </el-col>
                </el-row>

              </el-col>
            </el-row>
          </template>
          <template v-else>
            <p>角色没有关联权限，无信息</p>
          </template>
        </template>

      </el-table-column>
      <el-table-column type="index"
                       label="序号"
                       width="350">
      </el-table-column>
      <el-table-column prop="roleName"
                       label="角色名称"
                       width="350">
      </el-table-column>
      <el-table-column prop="roleDesc"
                       label="角色描述"
                       width="350">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="success"
                     round
                     size="mini"
                     icon="el-icon-edit"></el-button>
          <el-button type="danger"
                     round
                     size="mini"
                     icon="el-icon-delete"></el-button>

          <el-tooltip class="item"
                      effect="dark"
                      :enterable='false'
                      content="分配角色"
                      placement="top">
            <el-button @click='openAuth (scope.row)'
                       type="warning"
                       round
                       size="mini"
                       icon="el-icon-setting"></el-button>
          </el-tooltip>

        </template>
      </el-table-column>
    </el-table>

    <el-dialog title='角色授权'
               :visible.sync='isAuthShow'
               @close='defCheck=[]'
               width='30%'>
      <!-- 树形控件 -->
      <el-tree :data='treeList'
               v-if='isAuthShow'
               node-key='id'
               ref='tree'
               show-checkbox
               default-expand-all
               :default-checked-keys='defCheck '
               :props="{label:'authName',children:'children'}"></el-tree>
      <span slot='footer'>
        <el-button @click='isAuthShow=false'>取消</el-button>
        <el-button type='primary'
                   @click='authSubmit'>确定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  name: 'Role',
  data() {
    return {
      list: [], //列表数据
      treeList: [], //树的数据
      isAuthShow: false,
      defCheck: [], //修改角色的权限信息
      changeRoleId: ''
    }
  },

  created() {
    this.getRolesList()
    this.getRightsList()
  },
  methods: {
    //获取权限列表
    async getRolesList() {
      const { data: res } = await this.$axios.get('/roles')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.list = res.data
    },
    //删除角色的指定权限
    async delTag(val, row) {
      //val是点击标签的数据，row是这一大行的数据
      const { data: res } = await this.$axios.delete(
        `/roles/${row.id}/rights/${val.id}`
      )
      row.children = res.data //res.data是当前角色的所有权限信息
      this.$message.success(res.meta.msg)
    },
    //删除角色的指定权限
    async authSubmit() {
      let rids = [
        ...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys()
      ]
      rids = rids.join(',')
      const { data: res } = await this.$axios.post(
        '/roles/' + this.changeRoleId + '/rights',
        {
          rids
        }
      )
      if (res.meta.status !== 200) return this.$message.error(res.meta, msg)
      this.$message.success(res.meta.msg)
      this.changeRoleId = ''
      this.isAuthShow = false
      this.getRolesList()
    },
    openAuth(row) {
      this.isAuthShow = true
      this.getLeafKeys(row, this.defCheck)
      this.changeRoleId = row.id
    },
    //递归方法
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      //递归调用
      node.children.forEach(item => this.getLeafKeys(item, arr))
    },
    async getRightsList() {
      const { data: res } = await this.$axios.get('/rights/tree')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.treeList = res.data
    }
  }
}
</script>

<style >
.line {
  border-bottom: 1px solid #ddd;
  padding: 10px;
  align-items: center;
  display: flex;
}
.line:last-child {
  border: none;
  padding-bottom: 0;
}
.el-tag {
  margin: 10px 10px;
}
</style>